package com.bawei.a2010mdm.delegate;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.util.Log;

import com.google.gson.Gson;
import com.lody.virtual.client.core.VirtualCore;
import com.lody.virtual.client.hook.delegate.ComponentDelegate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import io.virtualapp.hook_sdk.dao.RiNotesService;
import io.virtualapp.hook_sdk.firewall.AppNetInfo;
import io.virtualapp.hook_sdk.firewall.FireWallType;
import io.virtualapp.hook_sdk.firewall.HookCallBack;
import io.virtualapp.hook_sdk.firewall.HookSDK;
import io.virtualapp.hook_sdk.firewall.HookType;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MyComponentDelegate implements ComponentDelegate, HookCallBack {

    private ArrayList<AppNetInfo> array = new ArrayList<>();//白名单/黑名单地址
    private String url = "https://www.baidu.com/";//hook后转发地址
//    private String url = "https://127.0.0.1/";//hook后转发地址

    @Override
    public void beforeApplicationCreate(Application application) {
//        List<UrlBean>datas = RiNotesService.getInstance().selUrl();
//        for (UrlBean bean :datas)
//            array.add(new AppNetInfo("",bean.hook_url,""));
        initData();
        Log.e("ZXY","MyComponentDelegate:"+array.size());
        Log.e("ZXY","MyComponentDelegate:"+RiNotesService.getInstance().selType());
        Log.e("ZXY","MyComponentDelegate:"+application.getPackageName());
        String whiteStr = RiNotesService.getInstance().selWhite();
        Log.e("ZXY","MyComponentDelegate:"+whiteStr);
        HookSDK.Build build = new HookSDK.Build();
        build.init(VirtualCore.get().getContext(),application.getPackageName(),this::violationUrl).hookUrl(url)
                .configFireWall(FireWallType.WHITE,array)
                .configHookType(HookType.All)
                .configwhiteStr(whiteStr)
                .build().hook();
    }

    private void initData(){
        array.add(new AppNetInfo("","http://passport.ewt360.com/",""));
        array.add(new AppNetInfo("","http://pv.sohu.com/",""));
        array.add(new AppNetInfo("","http://static.ewt360.com/",""));
        array.add(new AppNetInfo("","http://study.ewt360.com/",""));
        array.add(new AppNetInfo("","http://api.cloud.ssapi.cn/",""));
        array.add(new AppNetInfo("","http://www.ekwing.com/",""));
        array.add(new AppNetInfo("","http://res.ekwing.com/",""));
        array.add(new AppNetInfo("","http://mapi.ekwing.com/",""));
        array.add(new AppNetInfo("","http://zgd.ekwing.com/",""));
        array.add(new AppNetInfo("","http://cdn-resource.ekwing.com/",""));
        array.add(new AppNetInfo("","http://api.weicistudy.com/",""));
        array.add(new AppNetInfo("","http://sdkapp.mobile.sina.cn/",""));
        array.add(new AppNetInfo("","http://api.weibo.cn/",""));
        array.add(new AppNetInfo("","http://h5.sinaimg.cn/",""));
        array.add(new AppNetInfo("","http://gw5.push.mcp.weibo.cn/",""));
        array.add(new AppNetInfo("","http://202.108.0.86/",""));
        array.add(new AppNetInfo("","http://tvax2.sinaimg.cn/",""));
        array.add(new AppNetInfo("","http://wx1.sinaimg.cn/",""));
        array.add(new AppNetInfo("","http://wx2.sinaimg.cn/",""));
        array.add(new AppNetInfo("","http://wx3.sinaimg.cn/",""));
        array.add(new AppNetInfo("","http://wx4.sinaimg.cn/",""));
        array.add(new AppNetInfo("","http://192.168.31.107/",""));
    }
    @Override
    public void afterApplicationCreate(Application application) {
    }

    @Override
    public void beforeActivityCreate(Activity activity) {

    }

    @Override
    public void beforeActivityResume(Activity activity) {

    }

    @Override
    public void beforeActivityPause(Activity activity) {

    }

    @Override
    public void beforeActivityDestroy(Activity activity) {

    }

    @Override
    public void afterActivityCreate(Activity activity) {
    }

    @Override
    public void afterActivityResume(Activity activity) {

    }

    @Override
    public void afterActivityPause(Activity activity) {

    }

    @Override
    public void afterActivityDestroy(Activity activity) {

    }

    @Override
    public void onSendBroadcast(Intent intent) {

    }

    @Override
    public void violationUrl(String url) {
        Log.e("ZXY","violationUrl:"+url);
        HashMap<String,Object>map = new HashMap<>();
        map.put("deviceId",1111111111111111110L);
        map.put("classStr","技术部");
        map.put("deviceModel","违规访问"+url);
        map.put("name","测试");
        map.put("reason","reason");
        map.put("rom","rom");
        map.put("session","session");
        map.put("time",System.currentTimeMillis()/1000);
        map.put("userId",11111111111111111L);
        Gson gson = new Gson();
        String jsonStr = gson.toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"),jsonStr);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.url("http://192.168.31.107:8080/violation/addViolation").post(body);
        builder.build().newCall(requestBuilder.build()).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("ZXY","onFailure");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.e("ZXY","onResponse");
            }
        });
    }
}
